const Service = require('egg').Service;

// const mock = require('mockjs')
// const { list } = mock.mock({
// 'list|4': [{ 
//     name:'@cword(5,8)',
//     title: '@ctitle',
//     'acreage|50-1200':1,
//     'price|1-15':1,
//     'sorts|10-100':1,
//     'state|1':['待审核','已审核'],
// }]
// 'list|2-4':[{
//     num:Math.random().toString().substr(2,6),
//     name:'@cname',
//     tel:/^1[3-9]\d{9}$/,
//     'type|1':['二手房出租','有房出租','我想买个房'],
//     'pay|5-20':1,
//     time:'@datetime'
// }]
// })

class HomeService extends Service {

    //// 审核管理
    async getAudit() {
        const { ctx, app } = this

        // list.forEach(item => {
        //     this.app.mysql.insert('audit', item)
        // })

        const { id, keyword, pageIndex, pageSize } = ctx.query;
        let sql = `select * from audit where 1=1 `
        let sqlCount = `select count(id) as total from audit where 1=1 `
        if (id) {
            sql += `and id=${id} `
            sqlCount += `and id=${id} `
        }
        if (keyword) {
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex - 1) * pageSize},${pageSize} `
        }
        const data = await app.mysql.query(sql)
        const [{ total }] = await app.mysql.query(sqlCount)
        // return data
        return {
            total,
            data
        }
    }

    //// 订单管理
    async getOrder() {
        const { ctx, app } = this

        // list.forEach(item => {
        //     this.app.mysql.insert('orders', item)
        // })

        const { id, keyword, pageIndex, pageSize } = ctx.query;
        let sql = `select * from orders where 1=1 `
        let sqlCount = `select count(id) as total from orders where 1=1 `
        if (id) {
            sql += `and id=${id} `
            sqlCount += `and id=${id} `
        }
        if (keyword) {
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex - 1) * pageSize},${pageSize} `
        }
        const data = await app.mysql.query(sql)
        const [{ total }] = await app.mysql.query(sqlCount)
        // return data
        return {
            total,
            data
        }
    }

     /// 编辑审核数据
     async editAudit() {
        const { ctx, app } = this
        const {id}=ctx.request.body
        const data=await this.app.mysql.update('audit',{...ctx.request.body,id})
        return data
    }

     /// 判断订单数据
     async switchOrder() {
        const { ctx, app } = this
        const { type } = ctx.query;
        const data = await this.app.mysql.select('orders', { // 搜索 post 表
            where: { type: type }, // WHERE 条件
        })
        return data
    }

    /// 判断审核数据
    async switchAudit() {
        const { ctx, app } = this
        const { type } = ctx.query;
        const data = await this.app.mysql.select('audit', { // 搜索 post 表
            where: { state: type }, // WHERE 条件
        });
        return data
    }

    /// 判断团购数据
    async switchGrop() {
        const { ctx, app } = this
        const { type } = ctx.query;
        const data = await this.app.mysql.select('grop', { // 搜索 post 表
            where: { state: type }, // WHERE 条件
        });
        return data
    }

    /// 删除审核数据
    async delAudit() {
        const { ctx, app } = this
        const data = await app.mysql.delete('audit', { id: ctx.params.id })
        return data
    }

    /// 删除订单数据
    async delOrder() {
        const { ctx, app } = this
        const data = await app.mysql.delete('orders', { id: ctx.params.id })
        return data
    }

    async searchAudit(){
        let {ctx,app}=this
        let {title}=ctx.query
        let sql=`select * from audit where title like "%${title}"`
        let data=await app.mysql.query(sql)
        return data
    }
}

module.exports = HomeService;